<template>
  <!-- 1数学几何计算/1三维向量Vector3/ -->
  <div @mousemove="clickObj" id="webgl" class="main"></div>
  <div class="gdpTag" id="tag">{{ name }}GDP：{{ GDP }}亿元</div>
  <div class="gdpTag" id="cityName"></div>
  <div class="PMTag" id="PMName">
    <div class="city">
      <span>城市：</span>
      <span class="cityName">{{ name }}</span>
    </div>
    <div class="line"></div>
    <div class="PM">
      <span>PM2.5:</span>
      <span class="PMValue cityName">{{ PMValue }}</span>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref } from "vue";
import { renderer, css3Renderer } from "./RenderLoop.js";
import { camera } from "./RendererCamera.js";
import { cirMeshGroup } from "./scene/mesh.js";
import { scene } from "./scene/index.js";
import { tag } from "./tag.js";
import * as THREE from "three";
var width = window.innerWidth;
var height = window.innerHeight;
window.onresize = function () {
  width = window.innerWidth;
  height = window.innerHeight;
  renderer.setSize(width, height);
  camera.aspect = width / height;
  // 渲染器执行render方法的时候会读取相机对象的投影矩阵属性projectionMatrix
  // 但是不会每渲染一帧，就通过相机的属性计算投影矩阵(节约计算资源)
  // 如果相机的一些属性发生了变化，需要执行updateProjectionMatrix ()方法更新相机的投影矩阵
  camera.updateProjectionMatrix();
};
var GDP = ref("");
var name = ref("");
var PMValue = ref("");
const clickObj = (event) => {
  const px = event.offsetX;
  const py = event.offsetY;
  //屏幕坐标px、py转WebGL标准设备坐标x、y
  //width、height表示canvas画布宽高度
  const x = (px / width) * 2 - 1;
  const y = -(py / height) * 2 + 1;
  //创建一个射线投射器`Raycaster`
  const raycaster = new THREE.Raycaster();
  raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
  const intersects = raycaster.intersectObjects(cirMeshGroup.children);
  if (intersects.length > 0 && intersects[0].object.name) {
    console.log("label", label);
    label.position.copy(intersects[0].object.position);
    label.scale.set(0.05, 0.05, 0.05);
    scene.add(label);
    PMValue.value = intersects[0].object.PM25value;
    name.value = intersects[0].object.name;
  } else {
    scene.remove(label);
  }
};
var label = null;
onMounted(() => {
  label = tag();
  document.getElementById("webgl").appendChild(renderer.domElement);
  document.getElementById("webgl").appendChild(css3Renderer.domElement);
});
</script>
<style scoped lang="less">
.gdpTag {
  padding: 5px 10px;
  font-size: 16px;
  position: absolute;
  background-color: rgba(25, 25, 25, 0.5);
  border-radius: 5px;
  color: #fff;
}
.PMTag {
  color: #fff;
  background: rgba(0, 0, 0, 0.2);
  padding: 6px 16px;
  position: absolute;
  .cityName {
    color: #00ffff;
  }
  .line {
    background: rgb(190, 190, 190);
    height: 1px;
    margin-top: 4px;
    margin-bottom: 4px;
  }
}
</style>